
# =============================================================
# File-Name: MainFigures.R
#	Date:  		7 November 2019
#	Author:		Patrick Bayer
#	Paper:		Patrick Bayer, Ryan Kennedy, Joonseok Yang, and Johannes Urpelainen: The Need for Impact Evaluation in Electricity Access Research
#	Purpose:  Code produces all plots
# =============================================================

rm(list=ls())


# =============================================================
# Variable coverage plot
# =============================================================

obs <- c(10,18,2,1,13)
ex <- c(6,4,3,2,6)

data <- rbind(obs,ex)
colnames(data) <- c("Energy", "Income", "Savings", "Business", "Education")
rownames(data) <- c("Observational","Experimental")

# Color settings: colorblind-friendly palette
# http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/#a-colorblind-friendly-palette
cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")


pdf("var_coverage.pdf",height=6,width=10)
par(mar=c(4,4,4,4))

barplot(data,legend=TRUE,ylim=c(0,20),ylab="Count",args.legend=list(x="topleft",bty="n",horiz=TRUE),main="Assessed outcome variables by method",col=c(cols[3],cols[2]),beside=TRUE)

dev.off()


# =============================================================
# Mosaic plot
# =============================================================


# Results of impact evaluations plot
desc <- c(1,6,37)
ex <- c(0,10,11)

data2 <- rbind(desc,ex)
colnames(data2) <- c("Negative", "Neutral", "Positive")
rownames(data2) <- c("Observational","Experimental")

pdf("impact_mosaic.pdf",height=6,width=6)
par(mar=c(4,4,4,4))


mosaicplot(x=data2,ylab="Impact evaluation results", xlab="Method type",
color=c(cols[1],cols[2],cols[3]),las=1,
main="Results of impact evaluation by method")

# Counts by category

text(x=.41,y=.975,labels="1 result",cex=.7)

text(x=.41,y=.875,labels="6 results",cex=.7)
text(x=.85,y=.875,labels="10 results",cex=.7)

text(x=.41,y=.25,labels="37 results",cex=.7)
text(x=.85,y=.25,labels="11 results",cex=.7)

dev.off()


# =============================================================
# Mosaic plot for grid studies
# =============================================================


# Results of impact evaluations plot
desc <- c(1,4,29)
ex <- c(0,3,2)

data2 <- rbind(desc,ex)
colnames(data2) <- c("Negative", "Neutral", "Positive")
rownames(data2) <- c("Observational","Experimental")

pdf("impact_mosaic_grid.pdf",height=6,width=6)
par(mar=c(4,4,4,4))


mosaicplot(x=data2,ylab="Impact evaluation results", xlab="Method type",
           color=c(cols[1],cols[2],cols[3]),las=1,
           main="Results of impact evaluation by method (grid only)")

# Counts by category

text(x=.5,y=.975,labels="1 result",cex=.7)

text(x=.5,y=.875,labels="4 results",cex=.7)
text(x=.93,y=.875,labels="3 results",cex=.7)

text(x=.5,y=.25,labels="29 results",cex=.7)
text(x=.93,y=.25,labels="2 results",cex=.7)

dev.off()



# =============================================================
# Mosaic plot for offgrid studies
# =============================================================


# Results of impact evaluations plot
desc <- c(0,2,8)
ex <- c(0,7,9)

data2 <- rbind(desc,ex)
colnames(data2) <- c("Negative", "Neutral", "Positive")
rownames(data2) <- c("Observational","Experimental")

pdf("impact_mosaic_offgrid.pdf",height=6,width=6)
par(mar=c(4,4,4,4))


mosaicplot(x=data2,ylab="Impact evaluation results", xlab="Method type",
           color=c(cols[1],cols[2],cols[3]),las=1,
           main="Results of impact evaluation by method (offgrid only)")

# Counts by category

text(x=.275,y=.875,labels="2 results",cex=.7)
text(x=.725,y=.875,labels="7 results",cex=.7)

text(x=.275,y=.25,labels="8 results",cex=.7)
text(x=.725,y=.25,labels="9 results",cex=.7)

dev.off()



# =============================================================
# Barplot for studies over time
# =============================================================


year <- seq(2000,2018,1)

obs <- c(0,0,0,0,0,2,1,0,0,1,0,2,1,4,3,1,4,4,0)
ex <- c(0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,5,1)

data <- rbind(obs,ex)
rownames(data) <- c("Observational","Experimental")

# Color settings: colorblind-friendly palette
# http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/#a-colorblind-friendly-palette
cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

barx <- barplot(data,axes=FALSE,xlab="",ylab="",ylim=c(0,10))
dev.off()

pdf("time.pdf",height=6,width=6)
par(mar=c(4,4,4,4))

barplot(data,legend=TRUE,ylim=c(0,10),ylab="Count",args.legend=list(x="topleft",bty="n",horiz=TRUE),main="Published studies over years by method",col=c(cols[3],cols[2]))

mtext(side=1,at=c(barx[1],barx[10],barx[19]),line=1,c(year[1],year[10],year[19]))


dev.off()

# =============================================================
# Barplot for impact evaluations by outcome 
# =============================================================

# Color settings: colorblind-friendly palette
# http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/#a-colorblind-friendly-palette
cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

pdf("impactbymethod_byimpact.pdf",height=6,width=9)
par(mar=c(4,4,4,4))

# Data (Energy; income; savings; business; education)
pos <- c(9,6,14,1,1,1,0,1,13,2)
neu <- c(1,0,3,3,1,2,1,1,0,4)


data <- rbind(neu,pos)
rownames(data) <- c("neutral","positive")
colnames(data) <- rep(c("Observational", "Experimental"),5)

barplot(data,axes=FALSE,axisnames=FALSE,ylim=c(0,20),ylab="Count",beside=FALSE,col=c(cols[2],cols[3]),legend=TRUE,
args.legend=list(x=(7.3-5.2)/2+5.2,y=-2,bty="n",horiz=TRUE,xjust=0.5),main="Impacts by outcome and method",space=c(0,0.1,0.5,0.1,0.5,0.1,0.5,0.1,0.5,0.1),cex.axis=0.85)
axis(side=2,at=c(0,5,10,15,20),tick=TRUE,cex.axis=0.85)
axis(side=1,at=c(0.5,1.6,3.1,4.2,5.7,6.8,8.3,9.4,10.9,12),
labels=rep(c("Obs", "Exp"),5),cex.axis=0.85)



lines(c(0,2.1),c(17.5,17.5),lwd=1)
lines(c(2.6,4.7),c(17.5,17.5),lwd=1)
lines(c(5.2,7.3),c(17.5,17.5),lwd=1)
lines(c(7.8,9.9),c(17.5,17.5),lwd=1)
lines(c(10.4,12.5),c(17.5,17.5),lwd=1)

text(x=2.1/2,y=18,labels="Energy expenditure",cex=0.75)
text(x=(4.7-2.6)/2+2.6,y=18,labels="HH income",cex=0.75)
text(x=(7.3-5.2)/2+5.2,y=18,labels="HH savings",cex=0.75)
text(x=(9.9-7.8)/2+7.8,y=18,labels="Business creation",cex=0.75)
text(x=(12.5-10.4)/2+10.4,y=18,labels="Education",cex=0.75)

dev.off()

